package com.cqc.zhbjcqc.activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;

import com.cqc.zhbjcqc.R;

/**
 * Created by ${cqc} on 2016/9/30.
 */

public class NewsDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private ImageView iv_back_top;
    private ImageButton imgBtn_text_size;
    private ImageButton imgBtn_share;
    private ProgressBar progressBar;
    private WebView webView;
    private String url;
    private WebSettings settings;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        context = this;
        url = getIntent().getStringExtra("url");

        findViews();
        initViews();
    }

    private void findViews() {
        iv_back_top = (ImageView) findViewById(R.id.iv_back_top);
        imgBtn_text_size = (ImageButton) findViewById(R.id.imgBtn_text_size);
        imgBtn_share = (ImageButton) findViewById(R.id.imgBtn_share);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        webView = (WebView) findViewById(R.id.webView);
    }

    private void initViews() {
        imgBtn_text_size.setOnClickListener(this);
        iv_back_top.setOnClickListener(this);

        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
            }
        });
        settings = webView.getSettings();
    }

    private void showPopWindow(View v) {
//        View view = View.inflate(context,R.layout.layout_popwindow, null);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_popwindow,null);
        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);

//        popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.lineColor)));
        popupWindow.showAsDropDown(v);

        Button btn_largest = (Button) view.findViewById(R.id.btn_largest);
        Button btn_larger = (Button) view.findViewById(R.id.btn_larger);
        Button btn_normal = (Button) view.findViewById(R.id.btn_normal);
        Button btn_smaller = (Button) view.findViewById(R.id.btn_smaller);
        Button btn_smallest = (Button) view.findViewById(R.id.btn_smallest);

        btn_largest.setOnClickListener(this);
        btn_larger.setOnClickListener(this);
        btn_normal.setOnClickListener(this);
        btn_smaller.setOnClickListener(this);
        btn_smallest.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_top:
                NewsDetailActivity.this.finish();
                break;
            case R.id.imgBtn_text_size:
                showPopWindow(view);
                break;
            case R.id.btn_largest:
                settings.setTextSize(WebSettings.TextSize.LARGEST);
                popupWindow.dismiss();
                break;
            case R.id.btn_larger:
                settings.setTextSize(WebSettings.TextSize.LARGER);
                popupWindow.dismiss();
                break;
            case R.id.btn_normal:
                settings.setTextSize(WebSettings.TextSize.NORMAL);
                popupWindow.dismiss();
                break;
            case R.id.btn_smaller:
                settings.setTextSize(WebSettings.TextSize.SMALLER);
                popupWindow.dismiss();
                break;
            case R.id.btn_smallest:
                settings.setTextSize(WebSettings.TextSize.SMALLEST);
                popupWindow.dismiss();
                break;
        }
    }
}
